<template>
  <el-form ref="postFrom" :model="postFrom" :rules="rules">
    <Sticky :class-name="'sub-navbar ' + postFrom.status">
      <el-button v-if="!isEdit" @click="showHelp">显示帮助</el-button>
      <el-button v-loading="loading" type="success" @click="submitFrom">{{
        isEdit ? '编辑电子书' : '新增电子书'
      }}</el-button>
    </Sticky>
    <div id="detail-container">
      <el-row>
        <Warning />
        <el-col :span="24">
          <!-- 上传控件的具体样式 -->
          <EBookUpload
            :fileList="fileList"
            :disabled="isEdit"
            @onSuccess="onUploadSuccess"
            @onRemove="onUploadRemove"
          >
          </EBookUpload>
        </el-col>
        <el-col :span="24">
          <el-form-item prop="title" style="margin-bottom: 40px;">
            <MDinput
              v-model="postFrom.title"
              :maxlenth="100"
              name="name"
              required
            >
              书名
            </MDinput>
          </el-form-item>
          <el-row>
            <el-col :span="12">
              <el-form-item
                prop="author"
                label="作者:"
                :label-width="labelWidth"
              >
                <el-input
                  v-model="postFrom.author"
                  placeholder="作者"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                prop="publisher"
                label="出版社:"
                :label-width="labelWidth"
              >
                <el-input
                  v-model="postFrom.publisher"
                  placeholder="出版社"
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item
                prop="language"
                label="语言:"
                :label-width="labelWidth"
              >
                <el-input
                  v-model="postFrom.language"
                  placeholder="语言"
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                prop="rootFile"
                label="根文件:"
                :label-width="labelWidth"
              >
                <el-input
                  v-model="postFrom.rootFile"
                  placeholder="根文件"
                  disabled
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item
                prop="filePath"
                label="文件路径:"
                :label-width="labelWidth"
              >
                <el-input
                  v-model="postFrom.filePath"
                  placeholder="文件路径"
                  disabled
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                prop="unzipPath"
                label="解压路径:"
                :label-width="labelWidth"
              >
                <el-input
                  v-model="postFrom.unzipPath"
                  placeholder="解压路径"
                  disabled
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="12">
              <el-form-item
                prop="coverPath"
                label="封面路径:"
                :label-width="labelWidth"
              >
                <el-input
                  v-model="postFrom.coverPath"
                  placeholder="封面路径"
                  disabled
                ></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item
                prop="originalName"
                label="文件名称:"
                :label-width="labelWidth"
              >
                <el-input
                  v-model="postFrom.originalName"
                  placeholder="文件名称"
                  disabled
                ></el-input>
              </el-form-item>
            </el-col>
          </el-row>

          <el-row>
            <el-col :span="24">
              <el-form-item
                prop="cover"
                label="封面:"
                :label-width="labelWidth"
              >
                <a :href="postFrom.cover" v-if="postFrom.cover" target="_blank">
                  <img :src="postFrom.cover" class="preview-img" />
                </a>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 目录 -->
          <el-row>
            <el-col :span="24">
              <el-form-item :label-width="labelWidth" label="目录:">
                <div
                  class="contents-wrapper"
                  v-if="contentsTree && contentsTree.length > 0"
                >
                  <el-tree
                    :data="contentsTree"
                    @node-click="onContentClick"
                  ></el-tree>
                </div>
                <span v-else>无</span>
              </el-form-item>
            </el-col>
          </el-row>
        </el-col>
      </el-row>
    </div>
  </el-form>
</template>
<script>
import Sticky from '@/components/Sticky'
import EBookUpload from '@/components/EBookUpload'
import MDinput from '@/components/MDinput'
import Warning from './Warning.vue'
import { createBook, getBook, updateBook } from '@/api/book'
const fields = {
  title: '标题',
  author: '作者',
  publisher: '出版社',
  language: '语言'
}
export default {
  name: 'Detail',
  components: {
    Sticky,
    Warning,
    EBookUpload,
    MDinput
  },
  props: {
    isEdit: Boolean
  },
  data() {
    const validateRequire = (rule, value, callback) => {
      if (value.length === 0) {
        callback(new Error(fields[rule.field] + '必须填写'))
      } else {
        callback()
      }
    }
    return {
      loading: false,
      fileList: [],
      postFrom: {
        status: 'draft'
      },
      labelWidth: '100px',
      contentsTree: [],
      rules: {
        title: [
          { required: true, message: '请输入书名', validate: validateRequire }
        ],
        author: [
          { required: true, message: '请输入作者', validate: validateRequire }
        ],
        publisher: [
          { required: true, message: '请输入出版社', validate: validateRequire }
        ],
        language: [
          { required: true, message: '请输入语言', validate: validateRequire }
        ]
      }
    }
  },
  computed: {},
  watch: {},
  created() {
    if (this.isEdit) {
      const fileName = this.$route.params.fileName
      this.getBookDetail(fileName)
    }
  },
  methods: {
    getBookDetail(fileName) {
      getBook({ fileName }).then(res => {
        console.log(res)
        this.setData(res.msg)
      })
    },
    onContentClick(data) {
      if (data.text) {
        window.open(data.text)
      }
    },
    setData(data) {
      const {
        title,
        author,
        publisher,
        language,
        rootFile,
        cover,
        url,
        fileName,
        coverPath,
        filePath,
        unzipPath,
        originalName,
        contentsTree,
        contents
      } = data
      this.contentsTree = contentsTree
      console.log(originalName, url)
      this.fileList = [{ name: originalName, url }]
      this.postFrom = {
        ...this.postFrom,
        title,
        author,
        publisher,
        language,
        rootFile,
        cover,
        url,
        originalName,
        fileName,
        coverPath,
        filePath,
        unzipPath,
        originalName,
        contents
      }
    },
    setDefault() {
      this.contentsTree = []
      this.fileList = []
      this.$refs.postFrom.resetFields()
    },
    onUploadSuccess(data) {
      this.setData(data)
    },
    onUploadRemove() {
      this.setDefault()
      this.loading = false
    },
    submitFrom() {
      const onSuccess = msg => {
        this.$notify({
          title: '操作成功',
          message: msg,
          type: 'success',
          duration: 2000
        })
        this.loading = false
        this.setDefault()
      }
      if (!this.loading) {
        this.loading = true
        this.$refs.postFrom.validate((valid, fields) => {
          if (valid) {
            const book = { ...this.postFrom }
            delete book.contentsTree
            delete book.status
            if (!this.isEdit) {
              // 新增
              createBook(book)
                .then(res => {
                  onSuccess(res.msg)
                })
                .catch(err => {
                  this.loading = false
                })
            } else {
              updateBook(book).then(res => {
                onSuccess(res.msg)
              })
            }
          } else {
            const message = fields[Object.keys(fields)[0]][0].message
            this.$message({
              message,
              type: 'error'
            })
            this.loading = false
          }
        })
      }
    },
    showHelp() {
      console.log('显示帮助。。。')
    }
  }
}
</script>
<style lang="scss" scoped>
#detail-container {
  padding: 20px;
  .preview-img {
    width: 200px;
    height: 270px;
  }
}
</style>
